#ifndef LISTNODE_H_
#define LISTNODE_H_

// typeof 关键字是 GNU C 编译器扩展
#define list_entry(ptr, type, member) \
((type*)((char*)(ptr) - (unsigned long)(&(((type*)0)->member))))

typedef struct list_node {
    struct list_node* prev;
    struct list_node* next;
}list_node;

static inline 
void __list_insert(list_node* newp, 
                    list_node* prev, 
                    list_node* next)
{
    newp->next = next;
    newp->prev = prev;
    prev->next = newp;
    next->prev = newp;
}

static inline void list_insert(list_node* head, list_node* newp)
{
    __list_insert(newp, head, head->next);
}

static inline void list_insert_tail(list_node* head, list_node* newp)
{
    __list_insert(newp, head->prev, head);
}

static inline void list_delete(list_node* delnode)
{
    delnode->prev->next = delnode->next;
    delnode->next->prev = delnode->prev;
    delnode->next = delnode;
    delnode->prev = delnode;
}

#endif//LITSNODDE_H_